﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using INSTA.GestionEcole.DAL;

namespace INSTA.EcoleGestion.FORMS
{
    public partial class Note : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          

            if (!IsPostBack)
            {
               
                    string promoProf = (string)Session["PromoProfesseur"];
                    if (promoProf.Contains("-"))
                    {
                        string[] tab = promoProf.Split(Convert.ToChar("-"));

                        List<string> final = new List<string>();

                        foreach (string s in tab)
                        {
                            if (!final.Contains(s))
                            {
                                final.Add(s);
                            }
                        }
                        string[] tabPromo = (string[])final.ToArray();
                        for (int i = 0; i < tabPromo.Length; i++)
                        {
                            DropDownList1.Items.Add(tabPromo[i]);
                        }
                    }
                    else
                    {
                        DropDownList1.Items.Add(promoProf);
                    }

                
               
                BindGrid();
             }
        }

        public void BindGrid()
        {

            
                 using (var context = new GestionInstaEntities())
                 {
              
                
                var query4 = (from p in context.Etudiant
                              where p.Promo ==DropDownList1.SelectedItem.Text
                              select new {p.Nom, p.Prenom, p.DateDeNaissance, p.email}).ToList();

              
                GridView1.DataSource = query4;
          
               GridView1.DataBind();
           
               
            }

            
           
        }

        protected void ButtonSoumissionNote(object sender, EventArgs e)
        {


        }

        protected void SelectionPromo(object sender, EventArgs e)
        {
           /* using (var context = new GestionInstaEntities())
            {

                string promoProf = (string) Session["PromoProfesseur"];
                string[] tab = promoProf.Split(Convert.ToChar("-"));
                for (int i = 0; i < tab.Length; i++)
                {
                    DropDownList1.Items.Add(tab[i]);
                }
                var query = from p in context.Promo
                            where p.numéroPromo == DropDownList1.SelectedItem.Text
                            select p;
                String promo = null;
                foreach (var prof in query)
                {
                    promo = prof.numéroPromo;
                }

                //DropDownList1.Items.Add("");
                //SDropDownList1.Items.Add(promoProf);
                GridView1.DataSource = query;
            }*/
           
            BindGrid();

        }
        
        

        

        private void InsertionNote(string idEleve,string noteEleve,string type,bool isUpdate)
        {
            //int idEtu = Convert.ToInt32(ID);
            using (var context = new GestionInstaEntities())
            {

                if (isUpdate)
                {

                    Notes n = new Notes();
                    n.Identifiant = idEleve;
                    n.NoteEtudiant = noteEleve;
                    n.Type = type;
                    context.Notes.Add(n);
                    context.SaveChanges();

                }


            }
        }

        protected void EditerLigne(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;

            BindGrid();
        }

        protected void MiseAjourDansLaBase(object sender, GridViewUpdateEventArgs e)
        {
            string noteCC = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;//noteCC
            string noteP = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;//noteP
            string nom = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;  //nom
            string prenom = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;  //prenom
            string dateNais = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;  //dateNaissance
            string email = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;  //email
            string identifiant = null;
            using (var context = new GestionInstaEntities())
            {



                var query = from p in context.Etudiant
                    where (p.Nom == nom) & (p.Prenom == prenom) 
                    select p;

                 foreach (var etu in query)
                 {
                    identifiant = Convert.ToString(etu.ID);
                     
                 }

        }
            if (noteCC != null & !noteCC.Equals(""))
            {
                InsertionNote(identifiant, noteCC, "CC", true);
            }
            if (noteP != null & !noteP.Equals(""))
            {
                InsertionNote(identifiant, noteP, "Projet", true);
            }

            GridView1.EditIndex = -1;
            BindGrid();

        }


        

        protected void AnnulerEdition(object sender, GridViewDeleteEventArgs e)
        {

            GridView1.EditIndex = -1;
            BindGrid();
        }


       



    }
}


    

